package org.seven.jrdp.commons.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.seven.jrdp.commons.config.ParamConfig;

public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String URL_PATTERN = "/image";

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String servletPath = request.getServletPath();
		String fileName = ParamConfig.upload + uri.replaceFirst(contextPath + servletPath, "");
		File file = new File(fileName);
		if (file.exists()) {
			InputStream in = new FileInputStream(file);
			IOUtils.copy(in, response.getOutputStream());
			in.close();
		}
	}
}